
function chglgmore() {
  var div = getEl("div_lginfo");
  if(!div) return;
  div.className = div.className=="lgbox"?"lgbox lgpop":"lgbox";
}

function showChgShop(e) {
  var div = getEl("div_chgshop");
  if(!div) return;
  if(div.style.display=="none") {
    div.style.display = "";
    document.onclick = hideChgShop;
  } else {
    hideChgShop();
  }
  e.cancelBubble = true;
}
function hideChgShop() {
  var div = getEl("div_chgshop");
  if(div) div.style.display = "none";
  document.onclick = null;
}
function chgShop(code) {
  getJson("chgShop?shopCode="+code, function(data){
    if(data.ret)
      window.location.reload();
    else
      window.alert(data.msg);
  });
}

function getNewMsg() {
  try {
    callAjax("getNewMsg", null, function(data){
      if(data.ret) {
        var sp = getEl("sp_msgnum");
        if(sp) sp.innerHTML = data.data;
      }
    }, null, function(){}, "json");
  } catch(e) {}
  window.setTimeout(getNewMsg, 30000);
}
window.setTimeout(getNewMsg, 10000);


var _mls;
function loadMenu() {
  getJson("getMenu", function(data){
    if(data.ret) {
      _mls = data.data.menu;
      setTopNav();
      initLeftNav(data.data.usemu);
      resetHeight();
    }
  });
}

function setTopNav() {
  var i, j, k, arr1, arr2, arr3;
  var arr = [];
  arr.push('<ul>');
  arr1 = getNavList(0);
  for(i=0; i<arr1.length; i++) {
    arr.push('<li onmouseover="showNav(this);" onmouseout="hideNav(this, event);"><div class="subNav" style="display:none;'+(i==6?'margin-left:-304px;':(i==7?'margin-left:-274px;':''))+'">');
    arr2 = getNavList(arr1[i].id);
    for(j=0; j<arr2.length; j++) {
      arr.push('<dl><dt>'+arr2[j].name+'</dt>');
      arr3 = getNavList(arr2[j].id);
      for(k=0; k<arr3.length; k++) {
        arr.push('<dd><a onclick="fmopen(\''+arr3[k].url+'\');">'+arr3[k].name+'</a></dd>');
      }
      arr.push('</dl>');
    }
    arr.push('</div><a onclick="setLeftNav('+arr1[i].id+');">'+arr1[i].name+'</a></li>');
  }
  arr.push('</ul>');
  getEl("div_topnav").innerHTML = arr.join('');
}

function getNavList(pid) {
  var i, arr = [];
  for(i=0; i<_mls.length; i++) {
    if(_mls[i].pid==pid) arr.push(_mls[i]);
  }
  return arr;
}

function initLeftNav(ul) {
  var i, arr = [];
  if(ul && ul.length>0) {
    arr.push('<dl class="aluse">');
    arr.push('<dt id="dtn_0"><a onclick="lfopen(0);">常用功能</a></dt>');
    arr.push('<dd id="ddn_0"><ul>');
    for(i=0; i<ul.length; i++) {
      arr.push('<li><a onclick="fmopen(\''+ul[i].url+'\');">'+ul[i].name+'</a></li>');
    }
    arr.push('</ul></dd></dl>');
  }
  arr.push('<dl id="dv_lfnav"></dl>');
  getEl("div_leftnav").innerHTML = arr.join('');
  _lastNpid = 0;
}

function setLeftNav(pid) {
  var i, j, arr = [];
  var pl = getNavList(pid);
  for(j=0; j<pl.length; j++) {
    arr.push('<dt id="dtn_'+pl[j].id+'"><a onclick="lfopen('+pl[j].id+');">'+pl[j].name+'</a></dt>');
    arr.push('<dd id="ddn_'+pl[j].id+'" style="display:none;"><ul>');
    var ml = getNavList(pl[j].id);
    for(i=0; i<ml.length; i++) {
      arr.push('<li><a onclick="fmopen(\''+ml[i].url+'\');">'+ml[i].name+'</a></li>');
    }
    arr.push('</ul></dd>');
  }
  getEl("dv_lfnav").innerHTML = arr.join('');
  if(pl.length>0) lfopen(pl[0].id);
}

var _lastNpid = -1;;
function lfopen(pid) {
  if(_lastNpid==pid) return;
  var dt = getEl("dtn_"+_lastNpid);
  if(dt) dt.className = "";
  var dd = getEl("ddn_"+_lastNpid);
  if(dd) dd.style.display = "none";
  _lastNpid = pid;
  dt = getEl("dtn_"+_lastNpid);
  if(dt) dt.className = "open";
  dd = getEl("ddn_"+_lastNpid);
  if(dd) dd.style.display = "";
}


function showLogin(b) {
  getEl("div_fwbg").style.display = b?"":"none";
  getEl("div_login").style.display = b?"":"none";
}

function login(fm) {
  postForm(fm.action, fm, function(data){
    if(data.ret) {
      loadMenu();
      fm.reset();
      showLogin(false);
    } else {
      window.alert(data.msg);
    }
  });
  return false;
}

function logout() {
  getJson("_logout", logoutRet);
}
function logoutRet(data) {
  if(data.ret) window.location.reload();
}

function open(url) {
  fmshow(url);
}


var _lastNav = null;
function showNav(aa) {
  var u = aa.firstChild;
  if(u && u.className=="subNav") {
    u.style.display = "";
    _lastNav = aa;
  }
}

function hideNav(aa, e) {
  var el;
  if(window.event)
    el = event.toElement;
  else
    el = e.relatedTarget;
  while(el && el!=_lastNav) el = el.parentNode;
  if(!el) aa.firstChild.style.display = "none";
}


var MAX_FRAME = 6;
var _fm_cur = 0;
var _fm_num = 0;
function fmshow(url, pifm, bo) {
  if(fmfind(url)) return;
  var tb = document.getElementById("tb_ifms");
  if(tb.rows[0].cells.length>=MAX_FRAME && !bo) {
    window.alert("同时打开太多,请先关闭几个");
    return;
  }
  _fm_num++;
  var td = tb.rows[0].insertCell(-1);
  td.id = "td_ifms_"+_fm_num;
  td.fmid = _fm_num;
  if(pifm && pifm.parentNode && pifm.parentNode.fmid) td.pid = pifm.parentNode.fmid;
  td.height = "100%";
  td.innerHTML = '<iframe width="100%" height="100%" frameborder="0" scrolling="auto" src="'+url+'" onload="fmsett(this,'+_fm_num+');"></iframe>';

  tb = document.getElementById("tb_tabs");
  td = tb.rows[0].insertCell(-1);
  td.id = "td_tabs_"+_fm_num;
  td.fmid = _fm_num;
  td.fmurl = url;
  td.innerHTML = '<span onclick="fmsel('+_fm_num+',event)"><img src="images/ic_loading.gif">正在加载...</span><a onclick="fmclose('+_fm_num+');">×</a>';

  fmsel(_fm_num);
}

function fmopen(url, pifm) {
  fmshow(url, pifm);
  if(_lastNav) _lastNav.firstChild.style.display = "none";
}

function fmfind(url) {
  var tb = document.getElementById("tb_tabs");
  for(var i=0; i<tb.rows[0].cells.length; i++) {
    if(tb.rows[0].cells[i].fmurl==url) {
      fmsel(tb.rows[0].cells[i].fmid);
      //重新加载
      var td = document.getElementById("td_ifms_"+tb.rows[0].cells[i].fmid);
      if(td) td.firstChild.src = url;
      return true;
    }
  }
  return false;
}

function fmsel(n, e) {
  if(_fm_cur==n) return;
  var nc = e && e.ctrlKey;
  var li, td;
  if(_fm_cur>0 && !nc) {
    li = document.getElementById("td_tabs_"+_fm_cur);
    if(li) li.className = "";
    td = document.getElementById("td_ifms_"+_fm_cur);
    if(td) td.style.display = "none";
  }
  _fm_cur = n;
  li = document.getElementById("td_tabs_"+_fm_cur);
  if(li) li.className = "sel";
  td = document.getElementById("td_ifms_"+_fm_cur);
  if(td) {
    td.style.display = "";
    td.firstChild.focus();
  }
  setspos();
}

function fmclose(n) {
  var td = document.getElementById("td_tabs_"+n);
  if(!td) return;
  var td2 = document.getElementById("td_ifms_"+n);
  if(td2) {
    var w = td2.firstChild.contentWindow;
    if(w && w.chkClose && !w.chkClose()) return;
    td2.firstChild.src = "about:blank";
    td2.parentNode.removeChild(td2);
  }
  var n2 = 0;
  if(td.previousSibling)
    n2 = td.previousSibling.fmid;
  else if(td.nextSibling)
    n2 = td.nextSibling.fmid;
  td.parentNode.removeChild(td);
  if(n==_fm_cur && n2>0) fmsel(n2);
  setscb();
}

function fmsett(f, n) {
  var tt = '完成';
  try {
    tt = f.contentWindow.document.title;
  } catch(e) {}
  if(tt=="") tt = "&nbsp;";
  var li = document.getElementById("td_tabs_"+n);
  if(li) {
    li.firstChild.innerHTML = tt;
    li.firstChild.title = tt;
  }
}

function fmclosf(ifm) {
  if(ifm && ifm.parentNode && ifm.parentNode.fmid) {
    var fmid = ifm.parentNode.fmid;
    var pid = ifm.parentNode.pid;
    fmclose(fmid);
  }
}

function fmcload(ifm) {
  if(ifm && ifm.parentNode && ifm.parentNode.fmid) {
    var fmid = ifm.parentNode.fmid;
    var pid = ifm.parentNode.pid;
    fmclose(fmid);
    var td = document.getElementById("td_ifms_"+pid);
    if(td) {
      var w = td.firstChild.contentWindow;
      if(w && w.reloadlist) w.reloadlist();
      fmsel(pid);
    }
  }
}

function getParentFrameWin(ifm) {
  if(ifm && ifm.parentNode && ifm.parentNode.fmid) {
    var pid = ifm.parentNode.pid;
    var td = document.getElementById("td_ifms_"+pid);
    if(td) {
      return td.firstChild.contentWindow;
    }
  }
}

function fmsleft() {
  var div = document.getElementById("dv_tabs");
  var tb = document.getElementById("tb_tabs");
  if(!div || !tb) return;
  if(div.scrollLeft>0) {
    div.scrollLeft -= 50;
  }
  setscb();
}
function fmsright() {
  var div = document.getElementById("dv_tabs");
  var tb = document.getElementById("tb_tabs");
  if(!div || !tb) return;
  if(div.scrollLeft+div.clientWidth<tb.clientWidth) {
    div.scrollLeft += 50;
  }
  setscb();
}

function setspos() {
  var div = document.getElementById("dv_tabs");
  var tb = document.getElementById("tb_tabs");
  var td = document.getElementById("td_tabs_"+_fm_cur);
  if(!div || !tb || !td) return;
  if(td.offsetLeft+td.clientWidth>div.scrollLeft+div.clientWidth)
    div.scrollLeft = td.offsetLeft+td.clientWidth-div.clientWidth;
  if(td.offsetLeft<div.scrollLeft)
    div.scrollLeft = td.offsetLeft;
  setscb();
}

function setscb() {
  var div = document.getElementById("dv_tabs");
  var tb = document.getElementById("tb_tabs");
  var dv = document.getElementById("dv_scb");
  if(!div || !tb || !dv) return;
  dv.childNodes[0].className = div.scrollLeft>0?"lfc":"lfg";
  dv.childNodes[1].className = (div.scrollLeft+div.clientWidth<tb.clientWidth)?"rhc":"rhg";
}

function hideMenu(el) {
  var lf_menu = document.getElementById("p_left");
  if(!lf_menu) return;
  var div = document.getElementById("p_main");
  if(lf_menu.style.display=="none") {
    lf_menu.style.display = "";
    el.className = "splr_s";
    if(div) div.style.marginLeft = "167px";
  } else {
    lf_menu.style.display = "none";
    el.className = "splr_h";
    if(div) div.style.marginLeft = "7px";
  }
}

function resetHeight() {
  var hh = 0;
  if(document.documentElement.clientHeight>0 && (hh==0 || document.documentElement.clientHeight<hh)) hh = document.documentElement.clientHeight;
  if(document.body.clientHeight>0 && (hh==0 || document.body.clientHeight<hh)) hh = document.body.clientHeight;
  var div = document.getElementById("div_leftnav");
  if(div) div.style.height = (hh-80-10)+"px";
  div = document.getElementById("dv_ifms");
  if(div) div.style.height = (hh-80-36-10 -10)+"px";

}

if(window.attachEvent) {
  window.attachEvent("onload", resetHeight);
  window.attachEvent("onresize", resetHeight);
} else if(window.addEventListener) {
  window.addEventListener("load", resetHeight, false);
  window.addEventListener("resize", resetHeight, false);
}

window.onbeforeunload = function(e) {
  //if(window.confirm("确定要退出吗?")) {
  //  logout();
  //}
  return "确定要退出吗?";
}
